package top.feiskyend.core.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.util.WebUtils;
import top.feiskyend.core.utils.CommonUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: YTF
 * @date: 2022/9/13 12:26
 * @version: 1.0
 * @email: 1712310435@qq.com
 * @description:
 */
@Controller
public class HomeController {

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @GetMapping("/")
    public String fromZeroToExpert(HttpServletRequest request, HttpServletResponse response, Model model){
        // 1.获取cookie
        Cookie cookie = WebUtils.getCookie(request, "cookie");
        // 2.是否有cookie，否的话添加cookie
        if (cookie == null){
            cookie = new Cookie("cookie", CommonUtil.generateUUID());
            //设置cookie过期时间，单位是秒 不设置默认关闭游览器自动删除
            // cookie.setMaxAge(24*60*60);
            //生效范围
            cookie.setPath("/");
            // 发送cookie
            response.addCookie(cookie);
            model.addAttribute("message","嗨，欢迎您来到 from zero to expert");
        } else {
            model.addAttribute("message","嗨，欢迎您再次到 from zero to expert");
        }
        return "fromZeroToExpert";
    }
}
